/**
* ToDo:
*	-Test to make it work
*	-Code another client in java using ADT
*	-Recognize device type/RAM/CPU for work balancing
*	-OPTIMIZE ?
*	-ADD FEATURES 6
*	-use GPU for calculation
*
*/
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <ws2tcpip.h>
#include <string>
#include "DBruteClient.h"

#define MESSAGE_SIZE 100
using namespace std;

/*****
 * StartClient() - a function to startup winsock and connect to a server
 */
int StartClient(const char* port, const char* serverIP)
{
    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(2,0),&wsadata)) == 0)
	{
        struct addrinfo hints, *res;
        int sockfd;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if (getaddrinfo(serverIP,port,&hints,&res) != 0){
            cout<<"Client: getaddrinfo unsuccessful." << endl;
        }

        if ( (sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1 ){
            cout<<"Client: Unable to create socket." << endl;
        }

		cout << "Connecting to server..\n";
        if ( (connect(sockfd,res->ai_addr,res->ai_addrlen)) != -1 ){
            cout<<"-Connection Established." << endl;
        }

		cout << "Connected!\n";

		return sockfd;
	}
	else
		return -1;
}



/*****
 * EndClient() - a function to shutdown a socket and clean up winsoc
 */
void EndClient(int clientSocket)
{
    closesocket(clientSocket);
    WSACleanup();
}

void ReadMessage(string message, string& minPass, string& maxPass, string& hash,
				 string& charset, string& hashingType)
{
	int sPos= message.find('\\', 0);
	if (sPos == string::npos)
	{
		cout << "Error reading the message\n";
	}

	minPass= message.substr(0,sPos-1);
	int nPos= message.find('\\', sPos+1);

	maxPass= message.substr(sPos+1, nPos-1);
	sPos= message.find('\\', nPos+1);

	hash= message.substr(nPos+1, sPos-1);
	nPos= message.find('\\', sPos+1);

	charset= message.substr(sPos+1, nPos-1);
	hashingType= message.substr(nPos+1);
}

int main()
{
    cout << "Starting Client\n";

    int mySocket=0;
    mySocket = StartClient("8000", "192.168.0.106");
    if (mySocket == -1)
    {
        EndClient(mySocket); // Hmm... an error occurred.  Shutdown our client and bail
        return 1;
    }
 
    int nBytes;
    char inMessage[MESSAGE_SIZE];
    char outMessage[MESSAGE_SIZE];

	nBytes = recv(mySocket, inMessage, sizeof(inMessage), 0); // Get a message from server.
    if (nBytes == SOCKET_ERROR)
    {
        cout << "Client: Error receiving the message\n";
    }
    else
    {
		//Message format: minPass\maxPass\hash\charset\hashtype
		string message= "";
		for(int i=0; i!=nBytes; ++i)
		{
			message+=inMessage[i];
		}

		string minPass, maxPass, hash, charset, hashingType;
		ReadMessage(message, minPass, maxPass, hash, charset, hashingType);
		cout << message;
		

    }
	    
	/*
	nBytes = send(mySocket, outMessage, sizeof(outMessage), 0);

    if (nBytes == SOCKET_ERROR)
    {
        cout << "Client: Error sending message.\n";
    }
    else
    {
        cout << "Client: Sending : %s\n", outMessage;
    } 
	*/

    EndClient(mySocket);    

    cout << "Client: Press any key to shut me down ...\n";
    getchar();
    
    return 0;
}
